package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortPoint {
    public static void main(String[] args) {
        List<Point> list=new ArrayList<>();
        list.add(new Point(3,4));
        list.add(new Point(1,12));
        list.add(new Point(7,1));
        list.add(new Point(3,16));
        list.add(new Point(3,2));
        list.add(new Point(6,8));
        System.out.println("原list:"+list);

        list.sort(new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                if(o1.getX()==o2.getX()) {
                    return o1.getY() - o2.getY();
                }else{
                    return o1.getX() - o2.getX();
                }
            }
        });
        list.sort((o1, o2) -> {
            if(o1.getX()==o2.getX()) {
                return o1.getY() - o2.getY();
            }else{
                return o1.getX() - o2.getX();
            }
        });
        System.out.println("新list:"+list);

       /*
        Collections.sort(list, new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {

                if(o1.getX()==o2.getX()) {
                    return o1.getY() - o2.getY();
                }else{
                    return o1.getX() - o2.getX();
                }


                //return (o1.getX()*o1.getX()+o1.getY()*o1.getY())-(o2.getX()*o2.getX()+o2.getY()*o2.getY());
            }
        });//自定义排序
        System.out.println("新list:"+list);

        */

    }
}
